package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.PO.train_resultPO;
import com.ruoyi.system.domain.dto.Train_ResultDTO;
import com.ruoyi.system.domain.targetBinding;
import com.ruoyi.system.mapper.BindingMapper;
import com.ruoyi.system.mapper.MemberMapper;
import com.ruoyi.system.mapper.TrainingDataMapper;
import com.ruoyi.system.service.trainingDataUploadService;
import org.apache.poi.hpsf.Decimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.naming.Binding;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Ly lilongfei
 * @date 2024/11/6
 * @Description
 */
@Service
public class trainingDataUploadServiceImpl implements trainingDataUploadService {

    @Resource
    private TrainingDataMapper trainingDataMapper;
    @Resource
    private MemberMapper memberMapper;
    @Resource
    trainingDataUploadServiceImpl trainingDataUploadServiceImpl;
    @Resource
    private BindingMapper bindingMapper;


    @Override
    public void save(Train_ResultDTO data) {
        train_resultPO resultPO = train_resultPO.builder()
                .createTime(data.getTime())
                .circle(Float.parseFloat(data.getCircle()))
                .isGood(Integer.valueOf(data.getIsGood()))
                .project(data.getProject2())
                .rangeId(Integer.valueOf(data.getRangeId()))
                .siteId(Integer.valueOf(data.getSiteId()))
                .targetNumber(data.getTargetNo())
                .athleteId(trainingDataUploadServiceImpl.getAthleteByTargetNumber(data.getTargetNo()).getAthleteId())
                .xPosition(Float.parseFloat(String.valueOf(data.getxPosition())))
                .yPosition(Float.parseFloat(String.valueOf(data.getyPosition())))
                .groupTime(Integer.valueOf(data.getGroupTime()))
                .athleteName(trainingDataUploadServiceImpl.getAthleteByTargetNumber(data.getTargetNo()).getAthleteName())
                .singleTime(Integer.parseInt(data.getGroupTime()) / 60)
                .sort(1)
                .trainId(1)
                .tranches(1)
                .build();
        trainingDataMapper.insertSingleTrainingData(resultPO);

    }

    @Override
    public void saveAll(List<Train_ResultDTO> trainingDataList) {
        List<train_resultPO> trainingDataPOS = new ArrayList<train_resultPO>();
        for (Train_ResultDTO data : trainingDataList) {
            train_resultPO temp = train_resultPO.builder()
                    .createTime(data.getTime())
                    .circle(Float.parseFloat(data.getCircle()))
                    .isGood(Integer.valueOf(data.getIsGood()))
                    .project(data.getProject2())
                    .rangeId(Integer.valueOf(data.getRangeId()))
                    .siteId(Integer.valueOf(data.getSiteId()))
                    .targetNumber(data.getTargetNo())
                    .athleteId(trainingDataUploadServiceImpl.getAthleteByTargetNumber(data.getTargetNo()).getAthleteId())
                    .xPosition(Float.parseFloat(String.valueOf(data.getxPosition())))
                    .yPosition(Float.parseFloat(String.valueOf(data.getyPosition())))
                    .groupTime(Integer.valueOf(data.getGroupTime()))
                    .athleteName(trainingDataUploadServiceImpl.getAthleteByTargetNumber(data.getTargetNo()).getAthleteName())
                    .singleTime(Integer.parseInt(data.getGroupTime()) / 60)
                    .sort(1)
                    .trainId(1)
                    .tranches(1)
                    .build();
            trainingDataPOS.add(temp);
        }
        trainingDataMapper.insertBatchTrainingData(trainingDataPOS);

    }
    @Cacheable(key = "#targetNumber",value = "athleteId")
    public targetBinding getAthleteByTargetNumber(Integer targetNumber) {
        return bindingMapper.getAthleteByTarget(targetNumber);
    }
}
